﻿using VIA.Integration;
using VIA.SiMoJi.Domain.Inspection;

namespace VIA.SiMoJi.Domain.Electronics
{
    /// <summary>
    /// 控制面板。
    /// </summary>
    public class Panel : ControlPanel, IRefreshing
    {
        public EventButton EmgButton { get; }
        /// <inheritdoc />
        public Panel(LightButton runButton, LightButton stopButton, LightButton resetButton, EventButton emgButton) : base(runButton, stopButton, resetButton)
        {
            EmgButton = emgButton;
            emgButton.Released += EmgButton_Released;
            Add(emgButton);
        }

        protected override async void RunButton_Pressed(object sender, EventArgs e)
        {
            var inspector = Root as Inspector;
            await inspector.InitializeAsync();
            Root.Run(RunningMode.Online);
        }
        private void EmgButton_Released(object? sender, EventArgs e)
        {
            try
            {
                (Root as Inspector).ForceStop();
            }
            catch
            {

            }
        }

        public void Reset()
        {
            ((LightButton)RunButton).Reset();
            ((LightButton)ResetButton).Reset();
            ((LightButton)StopButton).Reset();
        }

        public void Refresh()
        {
            //(RunButton as LightButton).IsLight = this.RunState == RunState.Running || RunButton.IsPressed;
            //(StopButton as LightButton).IsLight = this.RunState == RunState.Stop || StopButton.IsPressed;
            //(ResetButton as LightButton).IsLight = this.RunState == RunState.Pause || ResetButton.IsPressed;
            EmgButton.Check();
            RunButton.Check();
            StopButton.Check();
            ResetButton.Check();
        }

        public void Refreshing()
        {
        }
    }
}
